package sync

import (
	"gddgame.cc/galaxy/common/dsl/game"
)

type SyncData struct {
	SyncLevels   []SettingSyncLevel
	SyncLevelMap map[int]*SettingSyncLevel
}

func GetSyncData(env *game.Environment) (*SyncData, error) {
	var err error
	data := &SyncData{}

	if data.SyncLevels, data.SyncLevelMap, err = getStatData(env); err != nil {
		return nil, err
	}

	return data, err
}
func getStatData(env *game.Environment) ([]SettingSyncLevel, map[int]*SettingSyncLevel, error) {
	var err error
	// stat
	var syncLevels []SettingSyncLevel

	model, err := env.GetModel(SettingSyncLevelName)
	if err != nil {
		return nil, nil, err
	}
	_, err = model.Query().Find(&syncLevels)
	if err != nil {
		return nil, nil, err
	}
	syncLevelMap := make(map[int]*SettingSyncLevel, len(syncLevels))
	for k := range syncLevels {
		syncLevelMap[syncLevels[k].Id] = &syncLevels[k]
	}
	return syncLevels, syncLevelMap, nil
}

func (data *SyncData) MappingLevel(ids []int) []uint {
	if ids == nil {
		return []uint{}
	}

	result := make([]uint, len(ids))
	for index, id := range ids {
		result[index] = data.SyncLevelMap[id].Level
	}
	return result
}
